//
//  IconButton.swift
//  AICleaner
//
//  Created by firefly on 2025/1/25.
//

import SwiftUI

enum TextDirection {
    // 文字在左
    case left
    // 文字在右
    case right
    // 文字在上
    case top
    // 文字在下
    case bottom
}

struct IconButton: View {
    var text: String = ""
    var imageName: String = ""
    /// 是否为系统图标
    var isSystemIcon: Bool = false
    var direction: TextDirection = .bottom
    var margin: Double = 3
    var textColor: Color?
    
    var body: some View {
        
        Group {
            switch direction {
            case .left:
                HStack {
                    Text(text)
                    Spacer().frame(width: margin)
                    
                    if isSystemIcon {
                        Image(systemName: imageName)
                    } else {
                        Image(imageName)
                    }
                        
                }
            case .right:
                HStack {
                    if isSystemIcon {
                        Image(systemName: imageName)
                    } else {
                        Image(imageName)
                    }
                    Spacer().frame(width: margin)
                    Text(text)
                }
            case .top:
                VStack {
                    Text(text)
                    Spacer().frame(height: margin)
                    if isSystemIcon {
                        Image(systemName: imageName)
                    } else {
                        Image(imageName)
                    }
                }
            case .bottom:
                VStack {
                    if isSystemIcon {
                        Image(systemName: imageName)
                    } else {
                        Image(imageName)
                    }
                    Spacer().frame(height: margin)
                    Text(text)
                }
            }
        }
        .foregroundColor(textColor ?? Color.white)
        
    }
}

extension IconButton {
    // 带箭头的button
    static func navigation(title: String,
                           imageName: String = "chevron.forward",
                           isSystemIcon:Bool = true,
                           color:Color = Color(hex:"2A75FF")!,
                           padding: Double = 8,
                           onPressed: (() -> Void)? = nil
    ) -> some View {
        IconButton(text: title,
                   imageName: imageName,
                   isSystemIcon: isSystemIcon,
                   direction: .left,
                   margin: .infinity)
        // 内部padding
        .padding(padding)
        .background(color)
        .borderRadius(8)
        .frame(maxWidth: .infinity)
        // 外部margin
        .padding(8)
        .onTapGesture {
            onPressed?()
        }
    }
}

#Preview {
    VStack {
        IconButton(text: "Battery", imageName: "battery",direction: .left, textColor: Color.black)
        IconButton(text: "Battery", imageName: "battery",direction: .right, textColor: Color.black)
        IconButton(text: "Battery", imageName: "battery",direction: .top, textColor: Color.black)
        IconButton(text: "Battery", imageName: "battery",direction: .bottom, textColor: Color.black)
    }
    
}
